package lc20240616;

import lc20240517.TreeNode;

/**
  * @description 数组转平衡二叉树
  * @author 不知名帅哥
  * @date 2024/6/16 23:39
  * @version 1.0
*/
public class ArrayToBST {
    public static void main(String[] args) {
        int[] nums={-10,-3,0,5,9};
        System.out.println(new ArrayToBST().sortedArrayToBST(nums));
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return buildTree(nums, 0, nums.length - 1);
    }

    private TreeNode buildTree(int[] nums, int start, int end) {
        if (start > end) {
            return null;
        }
        int mid = (start + end) / 2;
        TreeNode treeNode = new TreeNode(nums[mid]);
        treeNode.left = buildTree(nums, start, mid - 1);
        treeNode.right = buildTree(nums, mid + 1, end);
        return treeNode;
    }
}
